﻿<?php
class Model_Room extends Zend_Db_Table_Abstract{
	protected $db;
	public function __construct() {
		$this->db = Zend_Registry::get ( "db" );
	}
	
	public function searchAvai($startTime = false, $endTime = false) {
		if (!$startTime) {
			$startTime = date('Y-m-d H:i:s', time());
		}
		if (!$endTime) {
			$endTime = date('Y-m-d 00:00:00', time() + 24 * 3600);
		}
		$allRooms = $this->db->query ( "SELECT id, name, price, status  FROM catalog WHERE type = 'Room'" )->fetchAll();
		
		$allByIds = array();
		foreach($allRooms as $r) {
			$r['startTime'] = $startTime;
			$r['endTime'] = $endTime;
			$r['statusName'] = $this->getStatus($r['status']);
			$allByIds[$r['id']] = $r;
			
			
		}
		$rooms = $this->db->query ( "SELECT distinct(productId), productName  FROM orderproduct WHERE productType = 'Room' AND NOT(endTime < '$startTime' and startTime < '$endTime')" )->fetchAll();
		foreach($rooms as $room) {
			$allByIds[$room['productId']]['status'] = '2';
		}
		return $allByIds;
	}
	
	public function getStatus($id) {
		if ($id == 1) return 'Sẵn sàng';
		if ($id == 2) return 'Đang sử dụng';
		if ($id == -1) return 'Đang sửa';
	}
	
	public function getPricing() {
		$allRooms = $this->db->query ( "SELECT id, price, parentId  FROM catalog WHERE type = 'Room'" )->fetchAll();
		$allRoomByIds = array();
		$allRoomTypes = $this->db->query ( "SELECT id, price, priceParams  FROM catalog WHERE type = 'RoomType'" )->fetchAll();
		$allRoomTypeByIds = array();
		foreach($allRoomTypes as $r) {
			$allRoomTypeByIds[$r['id']] = $r;
		}
		foreach($allRooms as $r) {
			if ($r['parentId']) {
				$priceParams = $allRoomTypeByIds[$r['parentId']]['priceParams'];
				if($priceParams) {
					$r = array_merge($r, json_decode($priceParams, true));
				}
			}
			$allRoomByIds[$r['id']] = $r;
		}
		return $allRoomByIds;
	}
}
?>